{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas\n",
    "df = pandas.read_csv('data/house-prices.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Home</th>\n",
       "      <th>Price</th>\n",
       "      <th>SqFt</th>\n",
       "      <th>Bedrooms</th>\n",
       "      <th>Bathrooms</th>\n",
       "      <th>Offers</th>\n",
       "      <th>Brick</th>\n",
       "      <th>Neighborhood</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>114300</td>\n",
       "      <td>1790</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>No</td>\n",
       "      <td>East</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>114200</td>\n",
       "      <td>2030</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>No</td>\n",
       "      <td>East</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>114800</td>\n",
       "      <td>1740</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>No</td>\n",
       "      <td>East</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>94700</td>\n",
       "      <td>1980</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>No</td>\n",
       "      <td>East</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>119800</td>\n",
       "      <td>2130</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>No</td>\n",
       "      <td>East</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Home   Price  SqFt  Bedrooms  Bathrooms  Offers Brick Neighborhood\n",
       "0     1  114300  1790         2          2       2    No         East\n",
       "1     2  114200  2030         4          2       3    No         East\n",
       "2     3  114800  1740         3          2       1    No         East\n",
       "3     4   94700  1980         3          2       3    No         East\n",
       "4     5  119800  2130         3          3       3    No         East"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>SqFt</th>\n",
       "      <th>Bedrooms</th>\n",
       "      <th>Bathrooms</th>\n",
       "      <th>Offers</th>\n",
       "      <th>Yes</th>\n",
       "      <th>East</th>\n",
       "      <th>North</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>114300</td>\n",
       "      <td>1790</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>114200</td>\n",
       "      <td>2030</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>114800</td>\n",
       "      <td>1740</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>94700</td>\n",
       "      <td>1980</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>119800</td>\n",
       "      <td>2130</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Price  SqFt  Bedrooms  Bathrooms  Offers  Yes  East  North\n",
       "0  114300  1790         2          2       2    0     1      0\n",
       "1  114200  2030         4          2       3    0     1      0\n",
       "2  114800  1740         3          2       1    0     1      0\n",
       "3   94700  1980         3          2       3    0     1      0\n",
       "4  119800  2130         3          3       3    0     1      0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "house = pandas.concat([df, pandas.get_dummies(df['Brick']), pandas.get_dummies(df['Neighborhood'])], axis = 1)\n",
    "del house['No']\n",
    "del house['West']\n",
    "del house['Brick']\n",
    "del house['Neighborhood']\n",
    "del house['Home']\n",
    "house.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X = house[['SqFt', 'Bedrooms', 'Bathrooms', 'Offers', 'Yes', 'East', 'North']]\n",
    "Y = house['Price'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      y   R-squared:                       0.869\n",
      "Model:                            OLS   Adj. R-squared:                  0.861\n",
      "Method:                 Least Squares   F-statistic:                     113.3\n",
      "Date:                Sun, 07 May 2017   Prob (F-statistic):           8.25e-50\n",
      "Time:                        15:43:21   Log-Likelihood:                -1356.7\n",
      "No. Observations:                 128   AIC:                             2729.\n",
      "Df Residuals:                     120   BIC:                             2752.\n",
      "Df Model:                           7                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [95.0% Conf. Int.]\n",
      "------------------------------------------------------------------------------\n",
      "const       2.284e+04   1.02e+04      2.231      0.028      2573.371  4.31e+04\n",
      "SqFt          52.9937      5.734      9.242      0.000        41.640    64.347\n",
      "Bedrooms    4246.7939   1597.911      2.658      0.009      1083.042  7410.546\n",
      "Bathrooms   7883.2785   2117.035      3.724      0.000      3691.696  1.21e+04\n",
      "Offers     -8267.4883   1084.777     -7.621      0.000     -1.04e+04 -6119.706\n",
      "Yes          1.73e+04   1981.616      8.729      0.000      1.34e+04  2.12e+04\n",
      "East       -2.224e+04   2531.758     -8.785      0.000     -2.73e+04 -1.72e+04\n",
      "North      -2.068e+04   3148.954     -6.568      0.000     -2.69e+04 -1.44e+04\n",
      "==============================================================================\n",
      "Omnibus:                        3.026   Durbin-Watson:                   1.921\n",
      "Prob(Omnibus):                  0.220   Jarque-Bera (JB):                2.483\n",
      "Skew:                           0.268   Prob(JB):                        0.289\n",
      "Kurtosis:                       3.421   Cond. No.                     2.38e+04\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "[2] The condition number is large, 2.38e+04. This might indicate that there are\n",
      "strong multicollinearity or other numerical problems.\n"
     ]
    }
   ],
   "source": [
    "import statsmodels.api as sm\n",
    "X2 = sm.add_constant(X)\n",
    "est = sm.OLS(Y, X2)\n",
    "est2 = est.fit()\n",
    "print(est2.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('SqFt',)\n",
      "('Bedrooms',)\n",
      "('Bathrooms',)\n",
      "('Offers',)\n",
      "('Yes',)\n",
      "('East',)\n",
      "('North',)\n"
     ]
    }
   ],
   "source": [
    "predictorcols = ['SqFt', 'Bedrooms', 'Bathrooms', 'Offers', 'Yes', 'East', 'North']\n",
    "import itertools\n",
    "for variables in itertools.combinations(predictorcols, 1):\n",
    "    print(variables)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "AICs = {}\n",
    "for k in range(1,len(predictorcols)+1):\n",
    "    for variables in itertools.combinations(predictorcols, k):\n",
    "        predictors  = X[list(variables)]\n",
    "        predictors2 = sm.add_constant(predictors)\n",
    "        est = sm.OLS(Y, predictors2)\n",
    "        res = est.fit()\n",
    "        AICs[variables] = res.aic    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#AICs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(('SqFt', 'Bedrooms', 'Bathrooms', 'Offers', 'Yes', 'East', 'North'),\n",
       "  2729.3189814012489),\n",
       " (('SqFt', 'Bedrooms', 'Bathrooms', 'Offers', 'Yes'), 2789.5148143560264),\n",
       " (('SqFt', 'Offers', 'East', 'North'), 2805.9290455915971),\n",
       " (('SqFt', 'Bedrooms', 'Bathrooms', 'East', 'North'), 2827.1498026886024),\n",
       " (('Bedrooms', 'Bathrooms', 'Offers', 'Yes', 'East'), 2837.9283737790702),\n",
       " (('Bedrooms', 'Bathrooms', 'Offers', 'Yes'), 2845.9732955595991),\n",
       " (('SqFt', 'Offers'), 2865.6942475349356),\n",
       " (('Bedrooms', 'Bathrooms', 'Offers', 'East'), 2874.0450207228523),\n",
       " (('Bedrooms', 'Bathrooms', 'Yes'), 2883.9535408052025),\n",
       " (('SqFt', 'Yes'), 2896.9093592727936),\n",
       " (('Bedrooms', 'North'), 2908.6992372764653),\n",
       " (('Bedrooms', 'Bathrooms'), 2916.0356899473968),\n",
       " (('Bathrooms',), 2936.1658574541634)]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "c = Counter(AICs)\n",
    "#c.most_common()\n",
    "c.most_common()[::-10]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
